/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.util;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileUtils {
    public static String readSmallFile(String filePath) {
        return readSmallFile(Paths.get(filePath));
    }

    public static String readSmallFile(Path filePath) {
        try {
            byte[] bytes = Files.readAllBytes(filePath);
            return new String(bytes, StandardCharsets.UTF_8);
        } catch (FileNotFoundException | NoSuchFileException e) {
            throw new RuntimeException(String.format("no such file: %s", filePath.toString()), e);
        } catch (Exception e) {
            throw new RuntimeException(String.format("read file: %s error", filePath.toString()), e);
        }
    }

    public static InputStream getConfFileInputStream(String fileName) throws IOException {
        File file = new File("conf/" + fileName);
        if (file.exists()) {
            return new FileInputStream(file);
        }
        // return new ClassPathResource(fileName).getInputStream();
        throw new IOException("not found file conf/" + fileName);
    }
}
